#include "stock20.h"

Stock::Stock()
{
	
	company = new char[1];
	company[0] = '\0';
	shares = 0;
	share_val = 0.0;
	total_val = 0.0;
}
Stock::Stock(const char* co, long n, double pr )
{
	int len = strlen(co);
	company = new char[len + 1];
	strcpy(company, co);

	if (n < 0)n = 0;
	else
	{
		shares = n;
	}
	share_val = pr;
	set_tot();
}
Stock::~Stock()
{
	delete[] company;
	company = NULL;
}
void Stock::buy(long num, double price)
{
	if (num < 0)
	{

	}
	else
	{
		shares += num;
		shares = price;
		set_tot();
	}
}
void Stock::sell(long num, double price)
{
	if (num < 0)
	{

	}
	else if(num>shares)
	{

	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
}
void Stock::update(double price)
{
	shares = price;
	set_tot();
}
//void Stock::show()const
//{
//	ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
//	streamsize prec = cout.precision(3);
//
//	cout << "Company: " << company << " shares:" << shares << endl;
//	cout << " share price:$" << share_val;
//	cout.precision(2);
//	cout << " Total Worth:$" << total_val << endl;
//	cout.setf(orig, ios_base::floatfield);
//	cout.precision(prec);
//}
const Stock& Stock::topval(const Stock& s)const
{
	if (s.total_val > total_val)
	{
		return s;
	}
	else
	{
		return *this;
	}
}

ostream& operator<<(ostream&os, const Stock& s)
{
	ios_base::fmtflags orig = os.setf(ios_base::fixed, ios_base::floatfield);
	streamsize prec = os.precision(3);

	os << "Company: " << s.company << " shares:" << s.shares << endl;
	os << " share price:$" << s.share_val;
	os.precision(2);
	os << " Total Worth:$" << s.total_val << endl;
	os.setf(orig, ios_base::floatfield);
	os.precision(prec);
	return os;
}
